<?php
/*
 * This file is part of phpep.
 * 
 * phpep
 * 
 * Copyright (C) 2007 Andrés Otárola Alvarado pgugged/cast -- otarola.andres@gmail.com
 * 
 * phpep is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 * 
 * phpep is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *    
 */
require_once 'Connection.class.php';
require_once APP_DIR.'core/DbAdapter.interface.php';

class OracleDb  extends Connection implements DbAdapter{

    function __construct($autoConnect = true) {
    	parent::__construct();
    	if($autoConnect)
    		$this->connect();
    }
    function __destruct(){
    	$this->disconnect();
    }
    
    public function get_primary_keys($table_name){
    	$table_name = strtoupper($table_name);
//    	$sql = "select column_name \"pk\" from User_cons_columns where constraint_name IN (select CONSTRAINT_NAME from ALL_CONSTRAINTS where CONSTRAINT_TYPE = 'P' and TABLE_NAME = '{$table_name}')";
    	$sql = "SELECT COLUMN_NAME \"pk\" FROM USER_CONS_COLUMNS WHERE CONSTRAINT_NAME IN (SELECT CONSTRAINT_NAME FROM ALL_CONSTRAINTS WHERE CONSTRAINT_TYPE = 'P' AND TABLE_NAME = '{$table_name}')";
    	$res = $this->query($sql);
    	while($row = $this->fetch_array($res)){
    		$tmp[] = strtolower($row['pk']);
    	}
    	return $tmp;     	
    }
    
    public function get_table_fields($table_name){
    	$table_name = strtoupper($table_name);
    	//$sql = "select COLUMN_NAME from user_cons_columns where table_name like '{$table_name}'";
    	$sql = "SELECT COLUMN_NAME FROM USER_TAB_COLUMNS WHERE table_name = '{$table_name}'";
    	$res = $this->query($sql);
    	while($row = $this->fetch_array($res)){
			foreach($row as $k => $v)
    			$fields[] = strtolower($row['column_name']);
    	}
		//$fields = array_unique($fields);
    	return $fields;
    }
        
    public function connect(){
    	$this->link = oci_pconnect(
    		$this->dataConnection->getUser(),
    		$this->dataConnection->getPass(),
    		$this->dataConnection->getHost(),
    		$this->link) or die($this->error());
    }
     
    public function call_stored_procedure($sp_routine){
    	$call = oci_parse($this->link,"call {$sp_routine}");
    	oci_execute($call);
    }
    
    public function errno(){}
    
    public function error(){
    	return oci_error($this->link);
    }
    
    public static function escape_string($string){}
    
    public function query($query){
    	$res = oci_parse($this->link, $query);
    	oci_execute($res, OCI_COMMIT_ON_SUCCESS);
    	if(!$res) throw new Exception($this->error());
    	return $res;
    }
    
    public function fetch_array($result, $array_type = OCI_ASSOC){
    	$res =  oci_fetch_array($result, $array_type);
	$tmp = null;
	if($res){
	  $tmp = array();
	  foreach($res as $k => $v)
	    $tmp[strtolower($k)] = strtolower($v);
	}
	return $tmp;
    }
    
    public function fetch_row($result){
    	return oci_fetch_row($result);
    }
    
    public function fetch_object($result){
        return oci_fetch_object($result);
    }
    
    public function num_rows($result){
        $counter = 0;
    	while($row = $this->fetch_array($result)) $counter++;
		return $counter;
    }
    
   	protected function disconnect(){
        return oci_close($this->link);
    }
}
?>
